from typing import Dict
import os

NET_SCRIPT_RULES = [
    {"script": "net_网络丢包.sh", "keywords": ["丢包", "packet loss", "网络"], "weight": 2},
    {"script": "net_网卡降速.sh", "keywords": ["网卡降速", "降速", "网卡"], "weight": 1},
    {"script": "net_端口耗尽.sh", "keywords": ["端口耗尽", "port", "耗尽"], "weight": 2}
]

def select_script_for_anomaly(anomaly: Dict) -> str:
    root_cause = anomaly.get("root_cause", "").lower()
    evidence = " ".join(anomaly.get("evidence", [])).lower()
    severity = anomaly.get("severity", 0)
    best_score = -1
    best_script = NET_SCRIPT_RULES[0]["script"]
    for rule in NET_SCRIPT_RULES:
        score = 0
        for kw in rule["keywords"]:
            if kw in root_cause or kw in evidence:
                score += rule["weight"]
        if severity >= 8:
            score += 2
        elif severity >= 6:
            score += 1
        score += len(anomaly.get("evidence", [])) * 0.5
        if score > best_score:
            best_score = score
            best_script = rule["script"]
    return best_script 